package com.java_exam.java_exam.Controller;

import com.java_exam.java_exam.Mapper_impl.AdminMapperImpl;
import com.java_exam.java_exam.component.MyLocaleResolver;
import com.java_exam.java_exam.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {
    @Autowired
    AdminMapperImpl adminMapper;

    @PostMapping("/loginin")
    public String loginin(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          Map<String,Object> map,
                          HttpSession session){
        if(!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
            List<Admin> adminList = adminMapper.QueryByName(username);
            if (adminList.get(0).getAdminPassword().equals(password)) {
                session.setAttribute("username",username);
                return "redirect:/loginin.html";
            } else {
                return "404";
            }
        }else{
            map.put("msg","用户名或密码为空");
            return "index";
        }


    }




    @Bean   //注册区域语言组件
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

}
